<?php

namespace app\api\controller\qsy;


use app\admin\controller\activity\Subject;
use app\admin\model\activity\ActivityType;
use app\admin\model\activity\InformationActivity;
use app\admin\model\activity\ProjectActivity;
use app\admin\model\activity\SubjectActivity;
use app\admin\model\qsy\Banner;
use app\admin\model\qsy\Indexbanner;
use app\admin\model\qsy\Story;
use app\admin\model\wanlshop\Shop;
use app\common\controller\Api;
use app\common\handler\TimeHandler;
use app\common\helpers\ImageUtil;
use app\common\model\Config;
use think\Model;

/**
 * 主题活动，项目活动，动态信息
 */
class Activity extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //活动类型
    public function typeList()
    {

        $list = (new ActivityType)->getActivityTypeList();
        $data = [];
        foreach ($list as $k =>$v){
            $child = ActivityType::field('id,label as name')->where(['status'=>'1','type'=>$k])->select();
            $data[]=[
                     "name"=> $v,
                     "key"=>$k,
                     "child"=> $child
                   ];
        }
        $this->success('获取成功', $data);
    }

    //活动列表
    public function list()
    {
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        $bigType = $this->request->param('bigType');//大类
        $smallType = $this->request->param('smallType');//小类
        $search = $this->request->param('search');//搜索
        $sort = $this->request->param('sort');//搜索
        $where = [
            'a.status'=> "1" ,
            's.status'=> 'normal',
        ];
        if($smallType){
            $where['type_id'] = $smallType;
        }
        if($search){
            $where['title'] = ['like','%'.$search.'%'];
        }
       // price_rise =价格升序，price_fall=价格降序，heat_rise =热度升序，heat_fall=热度降序
        switch ($sort){
            case 'price_rise':
                $order = 'price asc,create_time DESC';
                break;
            case 'price_fall':
                $order = 'price DESC,create_time DESC';
                break;
            case 'heat_rise':
                $order = 'sold_num asc,create_time DESC ';
                break;
            case 'heat_fall':
                $order = 'sold_num DESC,create_time DESC';
                break;
            default:
                $order = 'create_time DESC';
                break;
        }


//        return ['project' => '娱乐项目', 'subject' => '主体活动'];
        if(in_array($bigType,['project','subject'])) {
            if($bigType == 'project'){

                $start_time = $this->request->param('start_time');//开始时间
                $end_time = $this->request->param('end_time');//结束时间
                if($start_time && $end_time){
                    $start_time = TimeHandler::ReplaceFormat($start_time);
                    $end_time = TimeHandler::getStrtotimeDate(strtotime($end_time)+86399);
                    $where['create_time'] = ['between',[$start_time,$end_time]];
                }

                $price = $this->request->param('price');
                if($price == 'free') {  //免费
                    $where['price']= '0.00';
                }
                if($price == 'charge') {  //收费
                    $where['price']= ['>',0];
                }
                $model=(new ProjectActivity)->alias('a')
                    ->field('a.id,cover_img,shopname,title,price,label,sketch,browse_num,sold_num,a.create_time')
                    ->join('wanlshop_shop s','s.id = a.shop_id','left')
                    ->where($where)
                    ->order($order)
                    ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
            }else{
                $model=(new SubjectActivity())->alias('a')
                    ->field('a.id,cover_img,shopname,title,sketch,label,a.address,start_time,end_time,a.create_time')
                    ->join('wanlshop_shop s','s.id = a.shop_id','left')
                    ->where($where)
                    ->order($order)
                    ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
            }

        } else{
            $this->error("大类参数错误");
        }
        $list =  $model->items();
        $total= $model->total();

        if($list){
            foreach ($list as $v){
                $v['cover_img'] = ImageUtil::handle($v['cover_img']);
                $v['labelAll'] = !in_array($v['label'],["",null]) ? explode(',',$v['label']) : [];
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("获取成功",$result);
    }

    //娱乐项目详情
    public function  projectDetails(){
        $id = $this->request->param('id');
        $info = ProjectActivity::field('id,shop_id,title,label,cover_img,browse_num,start_time,end_time,phone,price,introduction,sold_num,address,lng,lat')->where('id',$id)->find();
        if($info){
            $info->browse_num = $info->browse_num +1;
            $info->save();
            $site = \think\Config::get("site");
            $info['site_name'] = $site['site_name'];
            $info['shop_name'] = Shop::get($info['shop_id'])['shopname'] ?? '';
            $info['cover_img'] = ImageUtil::handle($info['cover_img']);
            $info['labelAll'] = !in_array($info['label'],["",null]) ? explode(',',$info['label']) : [];
            $this->success('获取成功', $info);
        }
        $this->error('不存在');
    }

    //主体详情
    public function  subjectDetails(){
        $id = $this->request->param('id');
        $info = SubjectActivity::field('id,shop_id,title,label,cover_img,start_time,end_time,phone,price,introduction,sold_num,address,lng,lat')->where('id',$id)->find();
        if($info){
            $site = \think\Config::get("site");
            $info['site_name'] = $site['site_name'];
            $info['shop_name'] = Shop::get($info['shop_id'])['shopname'] ?? '';
            $info['cover_img'] = ImageUtil::handle($info['cover_img']);
            $info['labelAll'] = !in_array($info['label'],["",null]) ? explode(',',$info['label']) : [];
            $this->success('获取成功', $info);
        }
        $this->error('不存在');
    }


    //动态列表
    public function informationList()
    {
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量

        $where = [
            'a.status'=> "1" ,
            's.status'=> 'normal',
        ];
        $model=(new InformationActivity())->alias('a')
            ->field('a.id,cover_img,shopname,title,price,introduction,start_time,end_time')
            ->join('wanlshop_shop s','s.id = a.shop_id','left')
            ->where($where)
            ->order('create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
        $total= $model->total();
        $list = $model->items();
        if($list){
            foreach ($list as $v){
                $v['cover_img'] = ImageUtil::handle($v['cover_img']);
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("获取成功",$result);
    }

    //动态详情
    public function  informationDetails(){
        $id = $this->request->param('id');
        $info = InformationActivity::field('id,shop_id,title,label,cover_img,start_time,end_time,phone,price,content,address,lng,lat,start_time,end_time')->where('id',$id)->find();
        if($info){
            $info['shop_name'] = Shop::get($info['shop_id'])['shopname'] ?? '';
            $info['cover_img'] = ImageUtil::handle($info['cover_img']);
            $info['labelAll'] = !in_array($info['label'],["",null]) ? explode(',',$info['label']) : [];
            $this->success('获取成功', $info);
        }
        $this->error('不存在');
    }


}
